<?php

namespace AdminLogin;

use Phalcon\Mvc\View;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;

class Module {

    public function __construct() {
        define('UPLOAD_DIR', realpath(dirname('index.php')) . DIRECTORY_SEPARATOR . 'tailieu' . DIRECTORY_SEPARATOR);
        define('REVIEW_DIR', realpath(dirname('index.php')) . DIRECTORY_SEPARATOR . 'review' . DIRECTORY_SEPARATOR);
    }

    public function registerAutoloaders() {

        $loader = new \Phalcon\Loader();

        $loader->registerNamespaces(array(
            'AdminLogin\Controllers' => '../app/adminlogin/controllers/',
            'Models' => '../app/models/',
            'Forms' => '../app/forms/',
            'Library' => '../app/library/',
        ));

        $loader->register();
    }

    /**
     * Register the services here to make them general or register in the ModuleDefinition to make them module-specific
     */
    public function registerServices($di) {

        $config = include __DIR__ . "/../config/config.php";

        //Registering a dispatcher
        $di->set('dispatcher', function () {
            $dispatcher = new \Phalcon\Mvc\Dispatcher();
//
//            //Attach a event listener to the dispatcher
//            $eventManager = new \Phalcon\Events\Manager();
//            $eventManager->attach('dispatch', new \Library\Acl('frontend'));
//            $dispatcher->setEventsManager($eventManager);
            $dispatcher->setDefaultNamespace("AdminLogin\Controllers\\");
            return $dispatcher;
        });

        /**
         * Setting up the view component
         */
        $di->set('view', function() use ($config) {

            $view = new View();

            $view->setViewsDir('../app/adminlogin/views/');

            $view->registerEngines(array(
                '.volt' => function($view, $di) use ($config) {

            $volt = new VoltEngine($view, $di);

            $volt->setOptions(array(
                'compiledPath' => '../app/adminlogin/cache/volt/',
                'compiledSeparator' => '_'
            ));
            //load function php
            $compiler = $volt->getCompiler();
            $compiler->addFunction('strtotime', 'strtotime');
            //define variable translate
            $compiler->addFunction('t', '_');
            return $volt;
        }
            ));

            return $view;
        }, true);
    }

}
